// 僵尸增强系统修复 - 已整合到zombie.js，此文件已禁用
(function() {
    'use strict';
    
    console.log('🔧 僵尸增强系统修复已禁用 - 功能已整合到主zombie.js文件');
    return; // 直接退出，避免冲突
    
    // 等待ZombieSpawner类加载完成
    setTimeout(() => {
        // 确保在关卡开始时正确初始化新僵尸类型
        const originalZombieSpawnerSpawnWave = window.ZombieSpawner ? window.ZombieSpawner.prototype.spawnWave : null;
        
        if (typeof ZombieSpawner !== 'undefined' && ZombieSpawner.prototype.spawnWave) {
            console.log('🔧 正在重写ZombieSpawner.spawnWave方法以支持新僵尸类型...');
        ZombieSpawner.prototype.spawnWave = function(level) {
            console.log(`🧟 生成第${level}关僵尸波次`);
            
            try {
                const levelManager = window.gameManager ? window.gameManager.levelManager : null;
                const levelData = levelManager ? levelManager.levels[level - 1] : null;
                
                if (!levelData) {
                    console.error(`找不到第${level}关的数据`);
                    return [];
                }
                
                const zombies = [];
                const zombieTypes = levelData.zombieTypes || ['normal'];
                const zombieCount = levelData.zombieCount || 5;
                
                console.log(`准备生成 ${zombieCount} 只僵尸，类型：`, zombieTypes);
                
                for (let i = 0; i < zombieCount; i++) {
                    const randomType = zombieTypes[Math.floor(Math.random() * zombieTypes.length)];
                    const spawnPoint = this.getRandomSpawnPoint();
                    
                    if (spawnPoint) {
                        try {
                            const zombie = new Zombie(spawnPoint.x, spawnPoint.y, randomType, level);
                            
                            // 确保新僵尸类型有正确的stats
                            if (!['normal', 'fast', 'tank', 'spitter'].includes(randomType)) {
                                zombie.stats = zombie.getZombieStats(randomType, level);
                                console.log(`✅ 创建新类型僵尸: ${randomType}`, zombie.stats);
                            }
                            
                            zombies.push(zombie);
                        } catch (error) {
                            console.error(`创建${randomType}僵尸失败:`, error);
                            // 如果新类型失败，创建一个普通僵尸作为备用
                            const fallbackZombie = new Zombie(spawnPoint.x, spawnPoint.y, 'normal', level);
                            zombies.push(fallbackZombie);
                        }
                    }
                }
                
                console.log(`✅ 成功生成 ${zombies.length} 只僵尸`);
                return zombies;
                
            } catch (error) {
                console.error('僵尸生成失败:', error);
                // 返回空数组避免游戏崩溃
                return [];
            }
        };
    } else {
        console.warn('⚠️ ZombieSpawner 不存在，跳过修复');
    }
    
    // 全局错误处理
    window.addEventListener('error', function(e) {
        if (e.message && e.message.includes('zombie') || e.message.includes('Zombie')) {
            console.error('🧟 僵尸相关错误:', e.message, e.filename, e.lineno);
            console.error('错误堆栈:', e.error ? e.error.stack : '无堆栈信息');
        }
    });
    
    // 添加调试命令
    window.debugZombieTypes = function() {
        console.log('🔍 调试僵尸类型:');
        console.log('当前场上僵尸:', window.gameEngine ? window.gameEngine.zombies.length : 0);
        
        if (window.gameEngine && window.gameEngine.zombies) {
            window.gameEngine.zombies.forEach((zombie, index) => {
                console.log(`僵尸 ${index + 1}:`, {
                    type: zombie.type,
                    health: zombie.health,
                    stats: zombie.stats,
                    x: zombie.x,
                    y: zombie.y
                });
            });
        }
    };
    
    // 添加测试新僵尸类型的函数
    window.testNewZombieType = function(type) {
        try {
            console.log(`🧪 测试僵尸类型: ${type}`);
            const testZombie = new Zombie(500, 300, type, 1);
            console.log('创建成功:', testZombie);
            console.log('Stats:', testZombie.stats);
            return testZombie;
        } catch (error) {
            console.error(`测试${type}僵尸失败:`, error);
            return null;
        }
    };
    
    // 在关卡开始时的额外检查
    const checkLevel = setInterval(() => {
        try {
            if (window.gameManager && 
                window.gameManager.levelManager && 
                window.gameManager.levelManager.currentLevel >= 11 &&
                window.gameEngine &&
                window.gameEngine.gameState === 'playing') {
                
                const currentLevel = window.gameManager.levelManager.currentLevel;
                console.log(`🎮 第${currentLevel}关运行中，僵尸数量: ${window.gameEngine.zombies ? window.gameEngine.zombies.length : 0}`);
                
                // 检查是否有新类型僵尸
                if (window.gameEngine.zombies) {
                    const newTypeZombies = window.gameEngine.zombies.filter(z => 
                        !['normal', 'fast', 'tank', 'spitter'].includes(z.type)
                    );
                    
                    if (newTypeZombies.length > 0) {
                        console.log(`🔥 发现 ${newTypeZombies.length} 只新类型僵尸`);
                        newTypeZombies.forEach(z => {
                            if (!z.stats) {
                                console.warn(`⚠️ 僵尸 ${z.type} 缺少stats，正在修复...`);
                                z.stats = z.getZombieStats(z.type, z.level || 1);
                            }
                        });
                    }
                }
            }
        } catch (error) {
            // 静默处理检查错误
        }
    }, 3000);
    
    // 5分钟后停止检查
    setTimeout(() => {
        clearInterval(checkLevel);
    }, 300000);
    
    console.log('✅ 僵尸增强系统修复已加载');
    
})();